#include "Stdafx.h"
#include "Message.h"
#include "Client.h"
#include <string.h>


Client* CMessage::m_pLogin = NULL;
Client* CMessage::m_pGs = NULL;

CMessage::CMessage()
{
	memset(&m_msg,0,sizeof(tagMsg));
	m_lsize = 0;
}
CMessage::CMessage(long type)
{
	memset(&m_msg,0,sizeof(tagMsg));
	m_lsize = 0;
	m_msg.type = type;
}

CMessage::~CMessage()
{

}

void CMessage::Add(char l)
{
	long size = sizeof(char);
	memcpy(m_msg.context+m_lsize,&l,size);
	m_lsize += size;
}

void CMessage::Add(unsigned char l)
{
	long size = sizeof(unsigned char);
	memcpy(m_msg.context+m_lsize,&l,size);
	m_lsize += size;
}

void CMessage::Add(short l)
{
	long size = sizeof(short);
	memcpy(m_msg.context+m_lsize,&l,size);
	m_lsize += size;
}

void CMessage::Add(unsigned short l)
{
	long size = sizeof(unsigned short);
	memcpy(m_msg.context+m_lsize,&l,size);
	m_lsize += size;
}

void CMessage::Add(long l)
{
	long size = sizeof(long);
	memcpy(m_msg.context+m_lsize,&l,size);
	m_lsize += size;
}

/*void CMessage::Add(__int64 l)
{
	long size = sizeof(__int64);
	memcpy(&m_msg.context+m_lsize,&l,size);
	m_lsize += size;
}
*/
void CMessage::Add(unsigned long l)
{
	long size = sizeof(unsigned long );
	memcpy(m_msg.context+m_lsize,&l,size);
	m_lsize += size;
}

void CMessage::Add(float l)
{
	long size = sizeof(float);
	memcpy(&m_msg.context+m_lsize,&l,size);
	m_lsize += size;
}

void CMessage::Add(const char* l)
{
	long size = strlen(l);
	Add(size);
	memcpy(m_msg.context+m_lsize,&l,size);
	m_lsize += size;
}

void CMessage::AddEx(void* l, long size)
{
	memcpy(m_msg.context+m_lsize,&l,size);
	m_lsize += size;
}

char  CMessage::GetChar()
{
	char l  = 0;
	long size = sizeof(char) ;
	memcpy(&l,m_msg.context+m_lsize, size);
	m_lsize += size;
	return l;
}

unsigned char CMessage::GetByte()
{
	unsigned char l  = 0;
	long size = sizeof(unsigned char) ;
	memcpy(&l,m_msg.context+m_lsize, size);
	m_lsize += size;
	return l;
}

short CMessage::GetShort()
{
	short l  = 0;
	long size = sizeof(short) ;
	memcpy(&l,m_msg.context+m_lsize, size);
	m_lsize += size;
	return l;
}

unsigned short CMessage::GetWord()
{
	unsigned short l  = 0;
	long size = sizeof(unsigned short) ;
	memcpy(&l,m_msg.context+m_lsize, size);
	m_lsize += size;
	return l;
}

long  CMessage::GetLong()
{
	long l  = 0;
	long size = sizeof(long) ;
	memcpy(&l,m_msg.context+m_lsize, size);
	m_lsize += size;
	return l;
}

/*__int64 CMessage::GetLONG64()
{
	__int64 l  = 0;
	long size = sizeof(__int64) ;
	memcpy(&l,&m_msg.context+m_lsize, size);
	m_lsize += size;
	return l;
}
*/
unsigned long CMessage::GetDWord()
{
	unsigned long l  = 0;
	long size = sizeof(unsigned long) ;
	memcpy(&l,m_msg.context+m_lsize, size);
	m_lsize += size;
	return l;
}

float CMessage::GetFloat()
{
	float l  = 0;
	long size = sizeof(float) ;
	memcpy(&l,m_msg.context+m_lsize, size);
	m_lsize += size;
	return l;
}

void* CMessage::GetEx(void* pData, long size)
{
	memcpy(pData,m_msg.context+m_lsize, size);
	m_lsize += size;
	return pData;
}

char* CMessage::GetStr( char* pStr,long lMaxLen )
{
	long size= GetLong();
	memcpy(pStr,m_msg.context+m_lsize, size);
	m_lsize += size;
	return pStr;
}

void CMessage::SendTo()
{
	m_msg.lsize = m_lsize + 8;
	m_pLogin->SendMessage(this);
}

void CMessage::SendToGS()
{
	m_msg.lsize = m_lsize + 8;
	m_pGs->SendMessage(this);
}

void CMessage::SetLogin(Client *login)
{
	m_pLogin = login;
}

void CMessage::SetGs(Client *Gs)
{
	m_pGs = Gs;
}

void  CMessage::SetMsg( const char* msg, long lSize)
{
	if( lSize > 0 )
	{
		memcpy(&m_msg,msg,lSize);
		m_lsize = 0;
	}
}